<?php
	session_start();
	
	if (!isset($_SESSION['sesi_login']))
		header("Location:../../index.php");

	@chdir("../../");
	define('ABS_URL',str_replace("\\","/",getcwd()));
	require ABS_URL.'/engine/Smarty.class.php';
	require ABS_URL."/functions/class.query.inc.php";
	$smarty = new Smarty();
	require ABS_URL."/dev_con.inc.php";
	require ABS_URL."/functions/func_file.php";
	$queryData = new queryData();
	$queryData->baseConfig();
	require ABS_URL."/functions/func_content.php";
	require ABS_URL."/langs/".BASE_LANGUAGE.'.inc.php';
	require $queryData->get_curr_dir(dirname(__FILE__))."/langs/".BASE_LANGUAGE.'.inc.php';

	if (isset($_POST['submit'])) {
		$judul = strip_tags($_POST['nama']);
		$deskripsi = strip_tags($_POST['keterangan']);
		$aext = getFileExtension($_FILES['namafile']['name']);
		$aext = strtolower($aext);
		$fileasli = $_FILES['namafile'];
		
		$a = array($judul,$deskripsi,$aext);
		$b = array('empty','empty','empty');
		$c = array($THEMES_TITLE,$THEMES_CONTENT,$THEMES_FILE);
		
		function getFileExtension($str) {
			$i = strrpos($str,".");
			if (!$i) { return ""; }
			$l = strlen($str) - $i;
			$ext = substr($str,$i+1,$l);
			return $ext;
		}
		
		$result = $queryData->NotificationCheck($a,$b,$c);
		if (empty($result)) {
			error_reporting(0);
			
			$dirme = "themes/";
			
			$fileaslix = explode(".",$_FILES['namafile']['name']);
					
			$newfile = $dirme.$fileasli;
	
			if (@move_uploaded_file($_FILES['namafile']['tmp_name'],$newfile)) {
				if (extract_zip($newfile, $dirme."/")) {
					
					$sqlz = "INSERT INTO tbl_themes VALUES('','".$_POST['nama']."','".$_POST['keterangan']."','".$fileaslix[0]."/','no','0')";
					$qryz = mysql_query($sqlz);
						
					unlink($newfile);
					$_SESSION['sesi_ver'] = $queryData->cekAksi($judul,'add');
				} else {
					$_SESSION['sesi_ver'] = $THEMES_FAIL_INSTALL;
					unlink($newfile);
				}
			} else {
				$_SESSION['sesi_ver'] = $THEMES_FAIL_INSTALL;
			}	
		} else {
			//variable pada [langs]
			$_SESSION['sesi_ver'] = $DEV_ACTION_NOTIFICATION;
			foreach($result as $data) {
				$_SESSION['sesi_ver'] .= '<br />- '.$data;
			}
			$act = 'tambah';
		}
	} else {	
		switch ($_GET['def']) {
			case "1" :
				$kode = (int) $_GET['kode'];
				($_GET['stat']=="yes") ? $stat = "yes" : $stat = "no";
				
				$sql = "UPDATE `tbl_themes` SET `defaultme`='no'";
				$qry = mysql_query($sql);
				
				$sql = "UPDATE `tbl_themes` SET `defaultme`='".$stat."' WHERE `idx`='".$kode."'";
				$qry = mysql_query($sql);
	
				$sql = "SELECT * FROM tbl_themes WHERE `defaultme`='yes'";
				$qry = mysql_query($sql);
				if (mysql_num_rows($qry)==0) {
					$sql = "UPDATE `tbl_themes` SET `defaultme`='yes' LIMIT 1";
					$qry = mysql_query($sql);
					$_SESSION['sesi_ver'] = $THEMES_DEFAULT_ACTIVE;
				} else {
					$_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'edit');
				}
	
			break;
			case "2" :
				$kode = (int) $_GET['kode'];
				if ($_GET['stat']=="yes") {
					$stat = "1";
				} else {
					$stat = "0";
				}
				$sql = "UPDATE `tbl_themes` SET `aktivasi`='".$stat."' WHERE `idx`='".$kode."'";
				$qry = mysql_query($sql);
			
				$sql = "SELECT * FROM tbl_themes WHERE `aktivasi`='1'";
				$qry = mysql_query($sql);
				if (mysql_num_rows($qry)==0) {
					$sql = "UPDATE `tbl_themes` SET `aktivasi`='1' LIMIT 1";
					$qry = mysql_query($sql);
					$_SESSION['sesi_ver'] = $THEMES_DEFAULT_ACTIVE;
				} else {
					$_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'edit');
				}
	
			break;
			case "3" :
				$kode = (int) $_GET['kode'];
				$sql = "SELECT * FROM `tbl_themes` WHERE `idx`='".$kode."' LIMIT 1";
				$qry = mysql_query($sql);
				
				while ($row = mysql_fetch_object($qry)) {
					del_dir("../../themes/".$row->url);
	
					$sqlx = "DELETE FROM `tbl_themes` WHERE `idx`='".$row->idx."'";
					$qryx = mysql_query($sqlx);
				}
				
				$_SESSION['sesi_ver'] = $queryData->cekAksi($kode,'delete');
			break;
		}
	}
	
	if (isset($_SESSION['sesi_login'])) {
		header('Location:../../?show='.SHOW.'&showview='.SHOWVIEW.'&act='.$act.'&limitdown=0');
	} else {
		header("Location:../../index.php");
	};
?>
